{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0.  1.  2.  3.]]\n",
      "\n",
      " [[ 4.  5.  6.  7.]]\n",
      "\n",
      " [[ 8.  9. 10. 11.]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[1.5]],\n",
       "\n",
       "       [[5.5]],\n",
       "\n",
       "       [[9.5]]], dtype=float32)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def GlobalAvgPool(x: np.ndarray):\n",
    "    x_reshaped = np.reshape(x, [x.shape[0], x.shape[1], -1])\n",
    "    y =  np.zeros([x.shape[0], x.shape[1], 1], dtype=x_reshaped.dtype);\n",
    "    for b in range(x_reshaped.shape[0]):\n",
    "        for c in range(x_reshaped.shape[1]):\n",
    "            y[b, c, 0] = np.sum(x_reshaped[b,c,...])/ x_reshaped.shape[2]\n",
    "    return y\n",
    "    # np.mean(x, axis=)\n",
    "x = np.arange(12).reshape(3, 1, 2, 2).astype(np.float32)\n",
    "GlobalAvgPool(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
